#! /bin/bash
#   
# memcached:    MemCached Daemon   
#   
# chkconfig:    - 90 25  
# description:  MemCached Daemon   
#   
### BEGIN INIT INFO
# Provides:          memcached
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:        $local_fs
# Should-Stop:        $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description:    memcached - Memory caching daemon
# Description:        memcached - Memory caching daemon
### END INIT INFO

PORT=11211
USER=daemon
MAXCONN=1024
CACHESIZE=64
OPTIONS=""
BIN="/usr/local/memcached/bin/memcached"
PID_FILE="/tmp/memcached.pid"
RETVAL=0
prog="Memcached"

start () {
    echo -n $"Starting $prog ..."
    $BIN -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P $PID_FILE $OPTIONS
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached && echo " Successful! " || echo " Failed! "
}

stop () {
    echo -n $"Stopping $prog ..."
    kill `cat $PID_FILE`
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        rm -f /var/lock/subsys/memcached
        rm -f $PID_FILE
	echo " Successful! "
    else
	echo echo " Failed! "
    fi
}

status () {
    if [ -f $PID_FILE ] ; then
	PID=`cat $PID_FILE`
	echo "$prog is running (PID:$PID) ..."
    else
        echo "$prog is stopped! "
    fi
}

restart () {
    stop
    start
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
	;;
    status)
	status
        ;;
    restart|reload)
        restart
        ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|reload}"
    exit 1
esac

exit $?
